package io.gitee.devaper.starlight.module.room

import io.gitee.devaper.starlight.module.room.websocket.*
import org.springframework.stereotype.Component
import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.concurrent.*
import kotlin.contracts.*

@Component
open class RoomRepository {

    val lock = ReentrantReadWriteLock(true)

    val map = HashMap<String, Room>()


    @OptIn(ExperimentalContracts::class)
    inline fun read(block: () -> Unit) {
        contract {
            callsInPlace(block, InvocationKind.EXACTLY_ONCE)
        }
        lock.readLock().withLock(block)
    }

    @OptIn(ExperimentalContracts::class)
    inline fun write(block: () -> Unit) {
        contract {
            callsInPlace(block, InvocationKind.EXACTLY_ONCE)
        }
        lock.writeLock().withLock(block)
    }

}